﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Service;
using S4Store.Models.SellerCarBrand;
using S4Store.Domain.SellerCarBrand;

namespace S4Store.Controllers
{
    public class SellerCarBrandController : ReportBaseController
    {
        public EmployeeService SellerService { get; set; }
        public StoreService StoreService { get; set; }
        public CarService CarService { get; set; }

        public SellerCarBrandService SellerCarBrandService { get; set; }

        private DateTime Month = DateTime.Now;

        public ActionResult Index()
        {

            using (var t = new Transtion())
            {
                t.Inject(StoreService);
                var store = StoreService.GetQuery().FirstOrDefault();

                ShowDataModel model = new ShowDataModel();
                model.Table.Month = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                model.Table.CurrentDate = DateTime.Now;

                t.Inject(SellerCarBrandService);
                model.Table = SellerCarBrandService.GetTable(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
                                                             store.Id);

                return View(model);
            }
        }


        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            using (var t = new Transtion())
            {
                t.Inject<StoreService>(StoreService);
                if (!StoreService.GetQuery().Any())
                {
                    throw new Exception("请先添加门店");
                }
                t.Commit();
            }
            if (Request["Month"] != null)
            {
                Month = DateTime.Parse(Request["Month"]);
            }

        }

        public ActionResult InputData()
        {
           
            InputDataModel model = new InputDataModel();
            model.Month = Month;
            model.Table.CurrentDate = DateTime.Now;
            using (var t = new Transtion())
            {
                t.Inject(SellerCarBrandService);
                model.Table = SellerCarBrandService.GetTable(Month.Year, Month.Month, DateTime.Now.Day, CurrentUser.StoreId.Value);
            }
            return View(model);
        }

        [HttpPost]
        public JsonResult Add(List<Row> rows)
        {
            using (var t = new Transtion())
            {
                t.Inject(SellerCarBrandService);
                SellerCarBrandService.Modify(rows);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }

    }
}
